/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.domain.payment;

import org.apache.commons.validator.CreditCardValidator;

/**
 * Provides methods to access credit card information.
 * 
 * @author Scott Rossillo
 *
 */
public final class CreditCardToolkit {
	
	private final static CreditCardValidator AMEX = new CreditCardValidator(CreditCardValidator.AMEX);
	private final static CreditCardValidator VISA = new CreditCardValidator(CreditCardValidator.VISA);
	private final static CreditCardValidator MASTERCARD = new CreditCardValidator(CreditCardValidator.MASTERCARD);
	private final static CreditCardValidator DISCOVER = new CreditCardValidator(CreditCardValidator.DISCOVER);
	
	/**
	 * Represents an American Express credit card type.
	 */
	public final static String TYPE_AMEX = "AMEX";
	
	/**
	 * Represents an Visa credit card type.
	 */
	public final static String TYPE_VISA = "VISA";
	
	/**
	 * Represents an MasterCard credit card type.
	 */
	public final static String TYPE_MASTERCARD = "MC";
	
	/**
	 * Represents an Discover credit card type.
	 */
	public final static String TYPE_DISCOVER = "DISC";
	
	/**
	 * Returns the brand for the given card number.
	 * 
	 * @param cardNumber the credit card number whose brand should be returned
	 * 
	 * @return the brand for the given <code>cardNumber</code> or <code>null</code> if the brand
	 * cannot be determined
	 */
	public static String getBrand(String cardNumber) {
		
		if(cardNumber == null) {
			return null;
		}
		
		if(AMEX.isValid(cardNumber)) {
			return TYPE_AMEX;
		} else if(VISA.isValid(cardNumber)) {
			return TYPE_VISA;
		} else if(MASTERCARD.isValid(cardNumber)) {
			return TYPE_MASTERCARD;
		} else if(DISCOVER.isValid(cardNumber)) {
			return TYPE_DISCOVER;
		} 
		
		return null;
	}

	
	private CreditCardToolkit() { }
}
